package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanBoXinXiZhiWeiSavePO;
import com.ct.application.shipping.model.po.ChuanBoXinXiZhiWeiSearchPO;
import com.ct.application.shipping.model.vo.ChuanBoXinXiZhiWeiVO;
import com.ct.application.shipping.service.ChuanBoXinXiZhiWeiService;
import com.ct.application.shipping.service.ChuanYuanDiaoPeiService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanBoXinXiZhiWeiMapper;
import com.ct.shipping.orm.ChuanBoXinXiZhiWeiDO;
import com.ct.shipping.orm.ChuanBoXinXiZhiWeiDO;
import com.ct.shipping.orm.ChuanYuanDiaoPeiDO;
import com.ct.utils.exception.ApiException;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 船舶信息职位中间表 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanBoXinXiZhiWeiServiceImpl extends BaseServiceImpl<ChuanBoXinXiZhiWeiMapper, ChuanBoXinXiZhiWeiDO>
        implements ChuanBoXinXiZhiWeiService{

    @Resource
    @Lazy
    private ChuanYuanDiaoPeiService chuanYuanDiaoPeiService;



    /**
     * 根据信息ID查询职位数量
     * @param xinxiId
     * @return
     */
    @Override
    public Map<Long,Integer> listChuanBoZhiweiXin(Long xinxiId){
        LambdaQueryWrapper<ChuanBoXinXiZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getXinXiId,xinxiId);
        List<ChuanBoXinXiZhiWeiDO> list = this.list(lambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            Map<Long,Integer> map = new HashMap<>();
            for (ChuanBoXinXiZhiWeiDO chuanBoXinXiZhiWeiDO : list) {
                map.put(chuanBoXinXiZhiWeiDO.getZhiWeiId(),chuanBoXinXiZhiWeiDO.getShuLiang());
            }
            return map;
        }
        return null;
    }

    /**
     * 查询职位分页列表
     */
    @Override
    public Page<ChuanBoXinXiZhiWeiVO> findChuanBoXinXiZhiWei(ChuanBoXinXiZhiWeiSearchPO searchPO) {
        LambdaQueryWrapper<ChuanBoXinXiZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(ChuanBoXinXiZhiWeiDO::getCreateTime);
        Page<ChuanBoXinXiZhiWeiDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanBoXinXiZhiWeiDO> page1 = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(page1, ChuanBoXinXiZhiWeiVO.class);
    }


    /**
     * 保存职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveChuanBoXinXiZhiWei(ChuanBoXinXiZhiWeiSavePO savePO){
        boolean saveFlag = false;
        LambdaQueryWrapper<ChuanBoXinXiZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getXinXiId,savePO.getXinXiId());
        lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getZhiWeiId,savePO.getZhiWeiId());
        long count = this.count(lambdaQueryWrapper);
        if(count == 0) {
            savePO.setId(null); //新增不需要id
            ChuanBoXinXiZhiWeiDO chuanBoZhiWeiDO = tranferBo(savePO, ChuanBoXinXiZhiWeiDO.class);
            if (Objects.nonNull(chuanBoZhiWeiDO)) {
                chuanBoZhiWeiDO.setSystemId(UserThreadLocal.get().getSystemId());
                chuanBoZhiWeiDO.setSystemName(UserThreadLocal.get().getSystemName());
                saveFlag = this.save(chuanBoZhiWeiDO);
            }
        }else{
            saveFlag = updateChuanBoXinXiZhiWei(savePO);
        }

        //同步数据到船员调配
        if (saveFlag) {
            List<ChuanBoXinXiZhiWeiDO> list = this.lambdaQuery()
                    .eq(ChuanBoXinXiZhiWeiDO::getXinXiId, savePO.getXinXiId())
                    .eq(ChuanBoXinXiZhiWeiDO::getZhiWeiId,savePO.getZhiWeiId())
                    .list();
            chuanYuanDiaoPeiService.saveChuanYuanDiaoPei(list);
        }
        return saveFlag;
    }

    /**
     * 修改职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChuanBoXinXiZhiWei(ChuanBoXinXiZhiWeiSavePO savePO){
        ChuanBoXinXiZhiWeiDO chuanBoZhiWeiDO = tranferBo(savePO, ChuanBoXinXiZhiWeiDO.class);
        if (Objects.nonNull(chuanBoZhiWeiDO)){
            LambdaQueryWrapper<ChuanBoXinXiZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getXinXiId,savePO.getXinXiId());
            lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getZhiWeiId,savePO.getZhiWeiId());
            ChuanBoXinXiZhiWeiDO chuanBoZhiWeiDO2 = this.getOne(lambdaQueryWrapper);
            if(Objects.nonNull(chuanBoZhiWeiDO2)) {
                chuanBoZhiWeiDO.setId(chuanBoZhiWeiDO2.getId());
                return this.updateById(chuanBoZhiWeiDO);
            }
        }
        return false;
    }


    /**
     * 删除职位
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanBoXinXiZhiWei(ChuanBoXinXiZhiWeiSavePO savePO){
        LambdaQueryWrapper<ChuanBoXinXiZhiWeiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getXinXiId,savePO.getXinXiId());
        lambdaQueryWrapper.eq(ChuanBoXinXiZhiWeiDO::getZhiWeiId,savePO.getZhiWeiId());
        ChuanBoXinXiZhiWeiDO chuanBoZhiWeiDO2 = this.getOne(lambdaQueryWrapper);
        if(chuanBoZhiWeiDO2!=null) {
            return this.removeById(chuanBoZhiWeiDO2.getId());
        }
        return false;
    }


    /**
     * 职位状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }


    @Override
    public ChuanBoXinXiZhiWeiVO getChuanBoXinXiZhiWei(Long id){
        ChuanBoXinXiZhiWeiDO chuanBoZhiWeiDO = getById(id);
        return tranferBo(chuanBoZhiWeiDO,ChuanBoXinXiZhiWeiVO.class);
    }

}
